//
//  ClockView.m
//  ClockView
//
//  Created by Mr.Q on 16/5/10.
//  Copyright © 2016年 QLS. All rights reserved.
//

#import "ClockView.h"

@implementation ClockView

#pragma mark - Public Methods

- (void)start{
    link = [CADisplayLink displayLinkWithTarget:self selector:@selector(updateClock:)];
    [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

}

- (void)stop{
    [link invalidate];
    link = nil;
}

#pragma mark - clock Style
- (void)setClockBackgroundImage:(CGImageRef)image{
    if (!image) {
        clock.contents = (__bridge id)[UIImage imageNamed:@"clock"].CGImage;
    }else{
        clock.contents = (__bridge id)image;
    }
}

- (void)setSecHandImage:(CGImageRef)image{
    if (!image) {
        secHand.backgroundColor = [UIColor redColor].CGColor;
    }else{
        secHand.contents = (__bridge id)image;
    }
}

- (void)setMinHandImage:(CGImageRef)image{

    if (!image) {
        minHand.backgroundColor = [UIColor blackColor].CGColor;
    }else{
        minHand.contents = (__bridge id)image;
    }
}

- (void)setHourHandImage:(CGImageRef)image{
    if (!image) {
        hourHand.backgroundColor = [UIColor blackColor].CGColor;
    }else{
        hourHand.contents = (__bridge id)image;
    }
}

#pragma mark - Private Methods

#define degree2angle(angle)    ((angle) * M_PI / 180)

- (void)updateClock:(CADisplayLink *)link{
    // 实例化日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获取当前时间的秒数
    NSDateComponents *comps = [calendar   components:NSCalendarUnitSecond fromDate:[NSDate date]];

    // 获取当前时间的分钟数
    NSDateComponents *minute = [calendar components:NSCalendarUnitMinute fromDate:[NSDate date]];

    // 获取当前时间的小时数
    NSDateComponents *hour = [calendar components:NSCalendarUnitHour fromDate:[NSDate date]];

    if (hour.hour > 12) hour.hour -=12; //PM

    // 设置秒针的旋转
    if (self.secRoll) {
        secHand.affineTransform = CGAffineTransformRotate(secHand.affineTransform, M_PI *2 /(60*60));

    }else{
        CGFloat angle = degree2angle(comps.second * 360/60);
        secHand.affineTransform = CGAffineTransformMakeRotation(angle);
    }
    // 分针
    CGFloat angleM = degree2angle(minute.minute * 360/60);
    minHand.affineTransform = CGAffineTransformMakeRotation(angleM);

    // 时针
    CGFloat angleH = degree2angle(hour.hour * 30 + minute.minute/10.0 * 5);
    hourHand.affineTransform = CGAffineTransformMakeRotation(angleH);

}

- (void)defaultSetup{

    clock = [[CALayer alloc]init];
    hourHand = [[CALayer alloc]init];
    minHand = [[CALayer alloc]init];
    secHand = [[CALayer alloc]init];

    // 默认样式
    [self setClockBackgroundImage:NULL];
    [self setSecHandImage:NULL];
    [self setMinHandImage:NULL];
    [self setHourHandImage:NULL];
    self.secRoll = YES; //默认扫秒

    // 添加控件
    [clock addSublayer:secHand];
    [clock addSublayer:minHand];
    [clock addSublayer:hourHand];
    [self.layer addSublayer:clock];
}

#pragma mark - layout

// WIDTH : 宽度 , LENGTH : 长度0.0~1.0 (比例)
#define SEC_HAND_WIDTH 2
#define SEC_HAND_LENGTH 0.8

#define MIN_HAND_WIDTH 3
#define MIN_HAND_LENGTH 0.7

#define HOUR_HAND_WIDTH 6
#define HOUR_HAND_LENGTH 0.6

#define ScreenSize self.frame.size

- (void)layoutSubviews{
    [super layoutSubviews];

    clock.bounds = CGRectMake(0, 0, ScreenSize.width, ScreenSize.height);
    clock.position = CGPointMake(ScreenSize.width/2, ScreenSize.height/2);

    secHand.position = minHand.position = hourHand.position = clock.position;

    //锚点
    //    clock.anchorPoint = CGPointMake(0, 0);
    CGPoint acp = CGPointMake(0.5, 0.8);
    secHand.anchorPoint = minHand.anchorPoint = hourHand.anchorPoint = acp;

    CGFloat scale = 1;
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        scale = [UIScreen mainScreen].scale;
    }

    float length = MIN(self.frame.size.width, self.frame.size.height)/2;
    CGFloat w,h;

    // 表针size
    if (hourHand.contents == NULL){
        w = HOUR_HAND_WIDTH;
        h = length*HOUR_HAND_LENGTH;
    }else{
        w = CGImageGetWidth((__bridge CGImageRef)hourHand.contents)/scale;
        h = CGImageGetHeight((__bridge CGImageRef)hourHand.contents)/scale;
    }
    hourHand.bounds = CGRectMake(0,0,w,h);

    if (minHand.contents == NULL){
        w = MIN_HAND_WIDTH;
        h = length*MIN_HAND_LENGTH;
    }else{
        w = CGImageGetWidth((__bridge CGImageRef)minHand.contents)/scale;
        h = CGImageGetHeight((__bridge CGImageRef)minHand.contents)/scale;
    }
    minHand.bounds = CGRectMake(0,0,w,h);

    if (secHand.contents == NULL){
        w = SEC_HAND_WIDTH;
        h = length*SEC_HAND_LENGTH;
    }else{
        w = CGImageGetWidth((__bridge CGImageRef)secHand.contents)/scale;
        h = CGImageGetHeight((__bridge CGImageRef)secHand.contents)/scale;
    }
    secHand.bounds = CGRectMake(0,0,w,h);

    // 判断指针转动样式
    if (self.secRoll) {
        // 如果是扫秒,初始化秒针位置
        [self secHandRoll];
    }

}

// 初始化秒针
- (void)secHandRoll{
    // 实例化日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];
    // 获取当前时间的秒数
    NSDateComponents *comps = [calendar   components:NSCalendarUnitSecond fromDate:[NSDate date]];
    CGFloat angle = degree2angle(comps.second * 360/60);
    //初始化秒针位置
    secHand.affineTransform = CGAffineTransformMakeRotation(angle);
}

#pragma mark - init
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        [self defaultSetup];

    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];

    [self defaultSetup];

}

- (void)removeFromSuperview
{
    [self stop];
    
    [super removeFromSuperview];
}

- (void)dealloc
{
    [self stop];
}
@end